<template>
	<div class="app-container" v-loading="loading">

		<div class="search-container  app_ny">
			<div class="div2" style="position: relative">
				<el-row :gutter="12" style="width: 100%">
					<el-col :span="3">
						<el-input v-model="queryParams.name" placeholder="请输入工程名称" clearable
							@keyup.enter="handleQuery" />

					</el-col>

					<el-col :span="6">
						<el-button :loading="loading" type="primary" @click="handleQuery"><template
								#icon><i-ep-search /></template>查询</el-button>
					</el-col>
				</el-row>
				<FilterColumn class="filtBox" @handleRefresh="resetQuery()" :strtype="strtype"></FilterColumn>
			</div>
		</div>



		<el-card shadow="never">
			<div class="action-row">
				<el-button v-hasPerm="['engineering.Engineering/save']" type="primary" @click="handleDo('','act_add')"
					plain><template #icon><i-ep-plus /></template>新增</el-button>
				<el-button @click="handleDo('', 'act_info')" :disabled="multipleSelection.length !== 1"
					:type="buttonType(multipleSelection.length)" plain><template
						#icon><i-ep-document /></template>详情</el-button>
				<el-button v-hasPerm="['engineering.Engineering/update']" @click="handleDo('', 'act_edit')"
					:disabled="multipleSelection.length !== 1" :type="buttonType(multipleSelection.length)"
					plain><template #icon><i-ep-edit /></template>修改</el-button>
				<el-button v-hasPerm="['engineering.Engineering/progress']" @click="handleDo('', 'act_progress',1)"
					:disabled="multipleSelection.length !== 1" :type="buttonType(multipleSelection.length)"
					plain><template #icon><i-ep-operation /></template>项目进度</el-button>
				<el-button v-hasPerm="['engineering.Engineering/delete']" @click="handleDo('', 'act_del')"
					:disabled="multipleSelection.length !== 1" :type="buttonType(multipleSelection.length)"
					plain><template #icon><i-ep-delete /></template>删除</el-button>
			</div>
			<el-table :key="reload" ref="dataTableRef" v-loading="loading" :data="tableData" align="left"
				tooltip-effect="dark" :show-overflow-tooltip="true" :scrollbar-always-on="true" border
				@cell-mouse-enter="rowMouse" @cell-mouse-leave="menuLineVisible=false"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" align="center" width="55" />
				<el-table-column type="index" label="序号" align="center" width="80" />
				<template v-for="(item,index) in cColumnsList()" :key='index'>
					<el-table-column :label="item.label" :align="item.align" :prop="item.prop" :width="item.width"
						:min-width="item.minwidth">
						<template #default="scope" v-if="item.prop == 'name'">
							<span>{{ scope.row.name }}</span>
						</template>
					</el-table-column>
				</template>
			</el-table>
			<pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.page"
				v-model:limit="queryParams.limit" @pagination="handleQuery" />

		</el-card>
		<!-- 添加工程 or 查看详情 -->
		<engineEdit v-if="dialogAdd.visible" v-model="dialogAdd.visible" :dialogTitle="dialogAdd.title"
			:dialogTableValue="dialogAdd.value" :dialogStatus="dialogAdd.status" @initRefreshList="handleQuery">
		</engineEdit>
		<!-- 工程进度 -->
		<engineProgressEdit v-if="dialogAdd2.visible" v-model="dialogAdd2.visible" :dialogTitle="dialogAdd2.title"
			:dialogTableValue="dialogAdd2.value" :dialogStatus="dialogAdd2.status" :dialogTableType="dialogAdd2.type">
		</engineProgressEdit>

	</div>
</template>
<script setup lang="ts">
	defineOptions({
		name: "Doc",
		inheritAttrs: false,
	});


	import router from '@/router';
	import engineEdit from "./components/engineEdit.vue";
	import engineProgressEdit from "./components/engineProgressEdit.vue";
	import { useTableFilterStore } from "@/store/modules/tableFilter";
	import { useUserStore } from "@/store/modules/user";
	const userStore = useUserStore();
	const TableFilter = useTableFilterStore();
	const strtype = ref('engine')
	//接口地址
	import {
		getEngineering,
		delEngineering,
	} from "@/api/engine";

	// 表格标题
	const cTitles = ["工程名称", "所属公司", '负责人','前期手续管理员','现场管理员', '添加时间'];
	// 显示字段
	const sTitles = ["工程名称", "所属公司", '负责人','前期手续管理员','现场管理员', '添加时间'];
	// 字段属性
	const cColumns = [

		{ label: "工程名称", prop: "name", align: "center", minwidth: 200 },
		{ label: "所属公司", prop: "company.name", align: "center", minwidth: 200 },
		{ label: "负责人", prop: "directorInfo.name", align: "center", minwidth: 150 },
		{ label: "前期手续管理员", prop: "qqsxInfo.name", align: "center", minwidth: 150 },
		{ label: "现场管理员", prop: "xcInfo.name", align: "center", minwidth: 150 },
		{ label: "添加时间", prop: "add_time_text", align: "center", minwidth: 150 }
	]

	interface TableGlobalLocal {
		[key : string] : any; // 或者更具体的类型
	}
	// 列表渲染
	function cColumnsList() {
		const ColumnsList = (TableFilter.tableGlobalLocal as TableGlobalLocal)[strtype.value + '_checklist'];
		if (ColumnsList) {
			const columnMap : any = {}; // 创建一个对象来存储cColumns中label和对象的映射  
			// 预先建立label到对象的映射  
			for (const column of cColumns) {
				columnMap[column.label] = column;
			}
			const newList = [];
			for (const label of ColumnsList) {
				// 尝试从映射中获取对象  
				const foundColumn = columnMap[label];
				if (foundColumn) {
					// 如果找到了，则添加到newList中  
					newList.push(foundColumn);
				}
			}
			return newList;
		}
		else {
			return [];
		}
	};
	// 列表-数据列初始化
	function columnsInit() {
		const dropColStr : string = strtype.value + '_dropCol';
		const checklistStr : string = strtype.value + '_checklist';
		const drop_col : any = (TableFilter.tableGlobalLocalTemp as TableGlobalLocal)[dropColStr];
		const checklist = (TableFilter.tableGlobalLocalTemp as TableGlobalLocal)[checklistStr];
		if (!drop_col) {
			(TableFilter.tableGlobalLocal as TableGlobalLocal)[dropColStr] = JSON.parse(JSON.stringify(cTitles));
			(TableFilter.tableGlobalLocalTemp as TableGlobalLocal)[dropColStr] = JSON.parse(JSON.stringify(cTitles));
		}
		if (!checklist) {
			(TableFilter.tableGlobalLocal as TableGlobalLocal)[checklistStr] = JSON.parse(JSON.stringify(sTitles));
			(TableFilter.tableGlobalLocalTemp as TableGlobalLocal)[checklistStr] = JSON.parse(JSON.stringify(sTitles));
		}
	}






	const queryFormRef = ref(ElForm);
	const queryParams = reactive({
		page: 1,
		limit: 20,
		name: "",
		username: "",
		cid: ""
	})

	const loading = ref(false);
	const ids = ref<number[]>([]);
	const total = ref(0);

	var reload = ref();
	const tableData = ref();
	const multipleSelection = ref<any>([]);
	const menuLineVisible = ref(true);
	const rowR = ref<any>({});

	const dialogAdd = reactive<any>({
		title: "添加信息", //弹窗标题
		visible: false,   //弹窗显示or隐藏
		status: "add_art", //弹窗状态类型  :添加 add_art 编辑 edit_art 详情 detail_art  
		value: {}   		  //携带整条参数 row
	});

	const dialogAdd2 = reactive<any>({
		title: "添加信息", //弹窗标题
		visible: false,   //弹窗显示or隐藏
		status: "add_art", //弹窗状态类型  :添加 add_art 编辑 edit_art 详情 detail_art  
		value: {},//携带整条参数 row
		type: 1
	});

	// 行菜单事件
	function rowMouse(row : any) {
		menuLineVisible.value = true
		rowR.value = row
	}
	// 数据多选事件
	function handleSelectionChange(val : any) {
		multipleSelection.value = val
	}
	// 操作按钮样式
	function buttonType(val : any) {
		if (val === 1) {
			return 'primary'
		} else {
			return 'info'
		}
	}



	// 操作
	function handleDo(row : any, act : string, intype ?: number) {
		// dialogStatus.value = act
		let uitem : any = {};
		switch (act) {
			case 'act_add':
				dialogAdd.visible = true
				dialogAdd.title = '添加信息'
				dialogAdd.status = act
				dialogAdd.value = {};
				break
			case 'act_edit':
				uitem = JSON.parse(JSON.stringify(row || multipleSelection.value[0]));
				dialogAdd.visible = true
				dialogAdd.title = '修改信息'
				dialogAdd.status = act
				dialogAdd.value = uitem;
				break
			case 'act_info':
				uitem = JSON.parse(JSON.stringify(row || multipleSelection.value[0]));
				dialogAdd.title = '查看详情';
				dialogAdd.status = act;
				dialogAdd.value = uitem;
				dialogAdd.visible = true;
				break
			case 'act_del':
				handleDelete(JSON.parse(JSON.stringify(row || multipleSelection.value[0])))
				break
			case 'act_progress':
				uitem = JSON.parse(JSON.stringify(row || multipleSelection.value[0]));
				dialogAdd2.title = '查看进度';
				dialogAdd2.status = act
				dialogAdd2.value = uitem;
				dialogAdd2.visible = true;
				dialogAdd2.type = intype
				break
			default:

		}
	}


	//获取工程信息列表
	function handleQuery() {
		loading.value = true;
		getEngineering(queryParams).then((res) => {
			console.log(res);
			tableData.value = res.data.list;
			total.value = res.data.count;
		}).finally(() => {
			loading.value = false;
		})
	}


	// 删除表单内容
	function handleDelete(row : any) {
		const Ids = row.id;
		if (!Ids) {
			ElMessage.warning("请勾选删除项");
			return;
		}
		ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			type: "warning",
		}).then(() => {
			loading.value = true;
			delEngineering({
				id: Ids
			}).then((res) => {
				console.log(res)
				ElMessage({
					message: '删除成功',
					type: 'success'
				})
				handleQuery();

			}).finally(() => {
				loading.value = false;
			})
		}, function () {
			console.log('取消删除')
		});
	}

	/**
	 * 重置查询
	 */
	function resetQuery() {
		handleQuery();
	}

	onMounted(() => {
		handleQuery();
		columnsInit();

	})
</script>
<style lang="scss" scoped>
	::v-deep .cuslabel {
		width: 100px !important;
	}

	::v-deep .el-timeline-item__timestamp {
		font-size: 18px;
		color: #409eff;
		font-weight: bold;
		margin-bottom: 16px;
		padding-top: 1px;
	}

	::v-deep .tabs-prog .el-tabs__header {
		margin-bottom: 30px;
	}

	.imgCus {
		width: 100px;
		height: 100px;
		margin-right: 10px;
	}
</style>